package com.ag.lzw.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ag.bean.TbMedicineConfirm;
import com.ag.bean.TbPropose;
import com.ag.bean.TbTask;
import com.ag.bean.TbUser;
import com.ag.common.BaseAction;
import com.ag.common.EventUtil;
import com.ag.common.IPermissionService;
import com.ag.lzw.dao.LzwTaskDAO;
import com.ag.lzw.dao.LzwTbProposeDAO;
import com.ag.lzw.service.MedicineConfirm;

@Scope("prototype")
@Controller("medicineConfirmAction")
public class MedicineConfirmAction extends BaseAction implements SessionAware{

	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger.getLogger(MedicineConfirmAction.class);
	/**
	 * 调用权限服务逻辑组件
	 */
	@Resource(name="PermissionServiceImpl")
	private IPermissionService pms;
	/**
	 * 
	 */
	@Resource(name="MedicineConfirm")
	private MedicineConfirm mcImpl;
	@Resource(name="lzwTbProposeDAO")
	LzwTbProposeDAO proposeDAO;
	@Resource(name = "lzwTaskDao")
	private LzwTaskDAO taskDAO;
	
	/**
	 * 提供Map<String, Object>类型的Session变量，用来保存Struts2注入的Session上下文
	 */
	private Map<String, Object> sessionMap;
	private String tid;
	private String cnum;
	private String mnum;
	private String dnum;
	private String wnum;
	private String ajaxMsg; 


	public String toMedicineConfirmPage(){
		TbUser currentUser = this.pms.getCurrentUser(sessionMap);
		String uid = currentUser.getUId(); // 获取当前用户ID
		logger.info("Current userID:" + uid);
		
		TbTask tt = taskDAO.findById(tid);

		if(isDeptLeader(tid,uid)){ //部门或分局负责人
			List<TbMedicineConfirm> list = this.mcImpl.queryChildMedicineConfirm(tid);
			this.getRequest().setAttribute("tmcs", list);
			return "deptLeaderGetMC";
		} else { //普通部门人员
			TbMedicineConfirm tmc = mcImpl.queryMedicineConfirm(tid);
			if(tmc!=null && tmc.getMcTime()!=null){ //若药品上报数据已被提交过，时间字段不为空
				logger.info("the MedicineConfirm record exited! its id is:"+tmc.getTId());
				this.getRequest().setAttribute("tmc", tmc);
				this.getRequest().setAttribute("taskname", tt.getTName());
				
				return "getMedicineConfirm_succ";
			} else if(tmc!=null) {
				logger.info("the MedicineConfirm record exit");
				this.getRequest().setAttribute("taskname", tt.getTName());
				this.getRequest().setAttribute("tmc", tmc);
				
				return "getMedicineConfirm_succ";
			} else {
				return "getMedicineConfirm_false";
			}
		}
	}
	
	public String toLeaderMCPage(){
		logger.info("it is leader query the MedicineConfirm!");
		List<TbMedicineConfirm> tmcs = mcImpl.queryLeaderMC(tid);
		this.getRequest().setAttribute("tmcs", tmcs);
		return "leaderGetMedicineConfirm";
	}
	
	public String saveMedicineConfirm(){
		this.mcImpl.saveMedicineConfirm(tid, cnum, mnum, dnum, wnum);
		this.ajaxMsg = "ok";
		return "saveMc_Succ";
	}
	
	private boolean isDeptLeader(String tid,String uid){

		TbTask tt = taskDAO.findTaskDetailById(tid);
		if(tt.getTbUserDept().getTbUser().getUId().equals(uid)){
			return true;
		} else {
			return false;
		}
		
	}
	
	private boolean isCurrentLeader(String uid){

		String eventID = null;
		// 根据传入的参数和当前用户的信息，判断当前用户是否为指挥长的角色
		logger.info("looking for leader...");
		try {
			if(this.getSessionAttribute(EventUtil.PROCESSING_EVENT_ID)!=null){
				eventID = getSessionAttribute(EventUtil.PROCESSING_EVENT_ID).toString(); // 获取当前处置事件的ID
				logger.info("Event ID:" + eventID);
				List list = proposeDAO.findEventLeader(eventID); // 查询当前事件的指挥长
				
				if (list != null) {
					for (Object tp : list) {
						 // 判断当前用户是否事件的指挥长
						
						String lid = ((TbPropose)tp).getTbUserByPViceCommander().getUId();
						logger.info("find event leader:"+lid);
						if (lid.equals(uid)) {
							logger.info("***It is Leader, TB_U_ID:" + lid);
							return true;
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return false;
	}
	
	@Override
	public void setSession(Map<String, Object> session) {
		this.sessionMap = session;	
	}

	public String getCnum() {
		return cnum;
	}

	public void setCnum(String cnum) {
		this.cnum = cnum;
	}

	public String getMnum() {
		return mnum;
	}

	public void setMnum(String mnum) {
		this.mnum = mnum;
	}

	public String getDnum() {
		return dnum;
	}

	public void setDnum(String dnum) {
		this.dnum = dnum;
	}

	public String getWnum() {
		return wnum;
	}

	public void setWnum(String wnum) {
		this.wnum = wnum;
	}

	public String getTid() {
		return tid;
	}

	public void setTid(String tid) {
		this.tid = tid;
	}
	
	public String getAjaxMsg() {
		return ajaxMsg;
	}

	public void setAjaxMsg(String ajaxMsg) {
		this.ajaxMsg = ajaxMsg;
	}

}
